package com.bitejiuyeke.biteportalservice.city.service.impl;

import com.bitejiuyeke.biteadminapi.map.domain.vo.RegionVO;
import com.bitejiuyeke.biteadminapi.map.feign.MapFeignClient;
import com.bitejiuyeke.bitecommoncore.utils.BeanCopyUtil;
import com.bitejiuyeke.bitecommondomain.domain.R;
import com.bitejiuyeke.bitecommondomain.domain.ResultCode;
import com.bitejiuyeke.biteportalservice.city.domain.vo.CityPageVO;
import com.bitejiuyeke.biteportalservice.city.service.ICityService;
import com.bitejiuyeke.biteportalservice.homepage.domain.vo.CityDescVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;

@Component
@Slf4j
public class CityServiceImpl implements ICityService {

    @Autowired
    private MapFeignClient mapFeignClient;

    @Autowired
    private Executor threadPoolTaskExecutor;
    @Override
    public CityPageVO getCityPage() {
        //异步编排
        //查询热门城市/全城市
        CompletableFuture<List<CityDescVO>> hotCityListFuture = CompletableFuture.supplyAsync(
                this::getHotCityList, threadPoolTaskExecutor
        );
        CompletableFuture<Map<String, List<CityDescVO>>> cityPyMapFuture = CompletableFuture.supplyAsync(
                this::getCityPyMap, threadPoolTaskExecutor
        );

        //等待异步任务全部完成
        CompletableFuture<Void> completableFuture = CompletableFuture.allOf(
                hotCityListFuture, cityPyMapFuture
        );
        try {
            completableFuture.get();
        } catch (Exception e) {
            log.error("异步并发调用出现异常");
        }

        //构造返回
        List<CityDescVO> hotCityList = hotCityListFuture.join();
        Map<String, List<CityDescVO>> cityPyMap = cityPyMapFuture.join();
        CityPageVO cityPageVO = new CityPageVO();
        cityPageVO.setHotCityList(hotCityList);
        cityPageVO.setAllCityMap(cityPyMap);
        return cityPageVO;
    }

    /**
     * 获取热门城市列表
     * @return
     */
    private List<CityDescVO> getHotCityList() {
        List<CityDescVO> result = new ArrayList<>();
        R<List<RegionVO>> r = mapFeignClient.getHotCityList();
        if(null ==  r
        || r.getCode() != ResultCode.SUCCESS.getCode()
        || null == r.getData()){
            log.error("获取城市列表失败");
        }else{
            result = BeanCopyUtil.copyListProperties(r.getData(), CityDescVO::new);
        }
        return result;
    }


    /**
     * 获取城市拼音列表
     * @return
     */
    private Map<String, List<CityDescVO>> getCityPyMap() {
        Map<String, List<CityDescVO>> result = new HashMap<>();
        R<Map<String, List<RegionVO>>> r = mapFeignClient.getCityPylist();
        if(null ==  r
        || r.getCode() != ResultCode.SUCCESS.getCode()
        || null == r.getData()){
            log.error("获取全城市映射失败");
        }else{
            for(Map.Entry<String, List<RegionVO>> entry : r.getData().entrySet()){
                result.put(entry.getKey(), BeanCopyUtil.copyListProperties(entry.getValue(), CityDescVO::new));
            }
        }
        return result;
    }
}
